package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.CareLevelDao;
import com.neusoft.SPNursingHome.po.CareLevel;
import com.neusoft.SPNursingHome.po.Client;
import com.neusoft.SPNursingHome.po.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CareLevelServiceImpl implements CareLevelService {
    @Autowired
    private CareLevelDao careLevelDao;

    @Override
    public ResponseBean<List<CareLevel>> listCareLevel() {
        System.out.println("调试：服务器运行");
        List<CareLevel> result = careLevelDao.selectList(null);
        ResponseBean<List<CareLevel>> responseBean = null;
        if (result == null) {
            responseBean = new ResponseBean<>(500, "获取护理级别列表错误");
        } else {
            responseBean = new ResponseBean<>(result);
        }
        return responseBean;
    }

//    @Override
//    public ResponseBean<List<CareLevel>> listCareLevelByStatus(Boolean careLevelStatus) {
//        System.out.println("调试：根据状态查询护理级别列表");
//        QueryWrapper<CareLevel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("care_level_status", careLevelStatus);
//        List<CareLevel> result = careLevelDao.selectList(queryWrapper);
//        ResponseBean<List<CareLevel>> responseBean = null;
//        if (result == null || result.isEmpty()) {
//            responseBean = new ResponseBean<>(500, "未找到符合条件的护理级别");
//        } else {
//            responseBean = new ResponseBean<>(result);
//        }
//        return responseBean;
//    }

    @Override
    public ResponseBean<IPage<CareLevel>> listCareLevelByStatus(Boolean careLevelStatus, long page, long size) {

        // 1. 分页对象
        Page<CareLevel> pager = new Page<>(page, size);

        System.out.println("调试：根据状态查询护理级别列表");
        QueryWrapper<CareLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("care_level_status", careLevelStatus);
        List<CareLevel> result = careLevelDao.selectList(queryWrapper);
        ResponseBean<List<CareLevel>> responseBean = null;
        if (result == null || result.isEmpty()) {
            responseBean = new ResponseBean<>(500, "未找到符合条件的护理级别");
        } else {
            responseBean = new ResponseBean<>(result);
        }
//        return responseBean;
        // 3. 执行分页查询
        return new ResponseBean<>(careLevelDao.selectPage(pager, queryWrapper));
    }

    @Override
    public ResponseBean<List<CareLevel>> listCareLevelByStatusNP(Boolean careLevelStatus) {


        System.out.println("调试：根据状态查询护理级别列表");
        QueryWrapper<CareLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("care_level_status", careLevelStatus);
        List<CareLevel> result = careLevelDao.selectList(queryWrapper);
        ResponseBean<List<CareLevel>> responseBean = null;
        if (result == null || result.isEmpty()) {
            responseBean = new ResponseBean<>(500, "未找到符合条件的护理级别");
        } else {
            responseBean = new ResponseBean<>(result);
        }
        return responseBean;
    }

    @Override
    public ResponseBean<Integer> updateCareLevelStatus(Integer careLevelId, Boolean newCareLevelStatus) {
        System.out.println("调试：修改护理级别状态");
        CareLevel careLevel = careLevelDao.selectById(careLevelId);
        if (careLevel == null) {
            return new ResponseBean<>(500, "未找到指定的护理级别");
        }
        careLevel.setCareLevelStatus(newCareLevelStatus);
        int updateResult = careLevelDao.updateById(careLevel);
        if (updateResult > 0) {
            return new ResponseBean<>(updateResult);
        } else {
            return new ResponseBean<>(500, "修改护理级别状态失败");
        }
    }

    @Override
    public ResponseBean<Integer> addCareLevel(String careLevelName, Boolean careLevelStatus) {
        System.out.println("调试：添加护理级别");

        CareLevel newCareLevel = new CareLevel();
        newCareLevel.setCareLevelName(careLevelName);
        newCareLevel.setCareLevelStatus(careLevelStatus);

        int saveResult = careLevelDao.insert(newCareLevel);
        if (saveResult > 0) {
            return new ResponseBean<>(saveResult);
        } else {
            return new ResponseBean<>(500, "添加护理级别失败");
        }
    }
}
